package com.cmwa.solution.business.prdSetup.standardPrjTemplate.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.prdSetup.standardPrjTemplate.entity.StandardPrjTemplateVo;
import com.cmwa.solution.business.prdSetup.standardPrjTemplate.service.StandardPrjTemplateService;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.cmwa.solution.sys.msg.util.StringUtil;
import com.google.common.base.Joiner;

import net.sf.json.JSONArray;

/**
 * 标准理财产品模板控制器
 * 
 * @author ex-zuotc
 * @date 2019-01-23 13:21:17
 */
@Controller
@RequestMapping("/business/prdSetup/standardPrjTemplate")
public class StandardPrjTemplateController extends BaseController {

	private final static String JSP_PATH = "business/prdSetup/standardPrjTemplate/";
	
	@Resource
	private StandardPrjTemplateService standardPrjTemplateService;
	@Resource
	private AttachService attachService;
		
	/**
	 * 跳转到列表页面
	 * @author ex-zuotc
	 * @date 2019-01-23 13:21:17
	 * @param model
	 * @return
	 */
	@RequestMapping("/standardPrjTemplateListView")
	public String standardPrjTemplateListView(ModelMap model) {
		return JSP_PATH + "standardPrjTemplateList";
	}

	@RequestMapping("/standardPrjTemplateListPage")
	@ResponseBody
	public Page<StandardPrjTemplateVo> standardPrjTemplateListPage(SearchParam param) {
		Page<StandardPrjTemplateVo> list = standardPrjTemplateService.standardPrjTemplateListPage(param);
		return list;
	}
	

	@RequestMapping("/addView")
	public String addView(@RequestParam(value = "id", required = false)String id, Model model) {
		return JSP_PATH + "standardPrjTemplateAdd";
	}
	
	@RequestMapping("/updateView")
	public String updateView(@RequestParam("id")String id, Model model) {
		StandardPrjTemplateVo prdTemplateVo = standardPrjTemplateService.getStandardPrjTemplateById(id);
		
		//加载附件信息
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "STANDARD_PRD_TEMPLATE");
		String attachList2Json = null;
		if(CollectionUtils.isNotEmpty(attachVos)){
			attachList2Json  = StringUtil.changeSpecialCharacter(JSONArray.fromObject(attachVos).toString());
		}
		
		model.addAttribute("prdTemplateVo", prdTemplateVo);
		model.addAttribute("attachList", attachList2Json);
		model.addAttribute("operType", "update");
		
		return JSP_PATH + "standardPrjTemplateAdd";
	}
	

	@RequestMapping("/save")
	public String save(StandardPrjTemplateVo standardPrjTemplateVo, @RequestParam("attIds")String attIds) {
		standardPrjTemplateService.saveStandardPrjTemplate(standardPrjTemplateVo, attIds);
		return redirectSuccess();
	}
	
	@RequestMapping("/detail")
	public String detail(@RequestParam("id")String id, Model model) {
		StandardPrjTemplateVo prdTemplateVo = standardPrjTemplateService.getStandardPrjTemplateById(id);
		
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "STANDARD_PRD_TEMPLATE");
		
		model.addAttribute("prdTemplateVo", prdTemplateVo);
		model.addAttribute("attachVos", attachVos);
		return JSP_PATH + "standardPrjTemplateDetail";
	}
	

	@RequestMapping("/delete")
	@ResponseBody
	public Result<StandardPrjTemplateVo> delete(@RequestParam("id")String id) {
		Result<StandardPrjTemplateVo> result = new Result<StandardPrjTemplateVo>();
		result.setSuccess(true);
		result.setMessage("删除成功");
		try {
			standardPrjTemplateService.logicDeleteStandardPrjTemplateById(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("删除失败");
		}
		return result;
	}
	
	/**
	 * 打包下载附件
	 * @param id
	 * @throws IOException 
	 */
	@RequestMapping("/download")
	public void download(@RequestParam("id")String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
		StandardPrjTemplateVo templateVo = new StandardPrjTemplateVo();
		templateVo.setId(id);
		List<StandardPrjTemplateVo> prdTemplateVo = standardPrjTemplateService.queryList(templateVo );
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "STANDARD_PRD_TEMPLATE");
		String fileName = "标准理财产品模板";
		if (CollectionUtils.isNotEmpty(prdTemplateVo)) {
			fileName = prdTemplateVo.get(0).getTemplateName();
			//如无特殊需求，调用公共批量下载方法
			if (prdTemplateVo.size() > 1) {
				fileName += "等";
			}
		}
		
		if (CollectionUtils.isNotEmpty(prdTemplateVo) && CollectionUtils.isNotEmpty(attachVos)) {
			List<String> attIds = new ArrayList<>();
			for (AttachVo attach : attachVos) {
				attIds.add(attach.getAttId());
			}
			attachService.downloadAttachZip(Joiner.on(",").join(attIds), fileName, request, response);
		} else {
			attachService.downloadAttachZip("", fileName, request, response);
		}
	}
}
